package com.zhihaoliang.animationtrain.transition

import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.transition.Transition
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.view.ViewCompat
import com.zhihaoliang.animationtrain.databinding.TransitionDetailActivityBinding

/**
 * 跳转的详情页面
 */

const val KEY_TRANSITION = "key_transition"
const val TRANSITION_IMAGE = "transition_image"
const val TRANSITION_TEXT = "transition_text"

private const val TAG = "TranDActivity"

class TransitionDetailActivity : AppCompatActivity() {

    private lateinit var binding: TransitionDetailActivityBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = TransitionDetailActivityBinding.inflate(layoutInflater).apply {
            setContentView(root)
        }

        binding.transitionBean= intent.getParcelableExtra(KEY_TRANSITION)
        ViewCompat.setTransitionName(binding.transitionTvTitle, TRANSITION_TEXT)
        ViewCompat.setTransitionName(binding.transitionIgvImage, TRANSITION_IMAGE)

        //Android 5.0(Lollipop) 及以上版本支持动画的监听功能
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            addTransitionListener()
        }
    }

    @RequiresApi(21)
    fun addTransitionListener() {
        val transition =
            window.sharedElementEnterTransition
        transition ?: return
        transition.addListener(object : Transition.TransitionListener {
            override fun onTransitionEnd(transition: Transition?) {
                Log.e(TAG, "onTransitionEnd")
            }

            override fun onTransitionResume(transition: Transition?) {
                Log.e(TAG, "onTransitionResume")
            }

            override fun onTransitionPause(transition: Transition?) {
                Log.e(TAG, "onTransitionPause")
            }

            override fun onTransitionCancel(transition: Transition?) {
                Log.e(TAG, "onTransitionCancel")
            }

            override fun onTransitionStart(transition: Transition?) {
                Log.e(TAG, "onTransitionStart")
            }

        })
    }
}
